﻿using AFanDa.BLL.BLLService;
using AFanDa.Dto.Request;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Data;

namespace AFanDa.Api.Controllers
{
    public class indexController : BaseController
    {
        LoginService _loginService = null;
        public indexController()
        {
            _loginService = new LoginService();
        }

        [HttpPost]
        public IHttpActionResult login(LoginDto dto)
        {

            // 生成code
            string code = Common.Config.GUID();
            // 判断是否登录正确
            var coderesult = _loginService.Logins(dto.account, dto.password, dto.code);
            // 如果返回值ok则登录正确

            return Ok(coderesult);



        }

        /// <summary>
        /// 前段登录
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult WxLogin(WxUserRequestDto dto)
        {
            var result = _loginService.WxLogin(dto);

            if (result.code == Dto.Base.ApiCode.ok)
            {
                // 定义要存储的信息 泛型字典 string 为 key ，object 为值
                var payload = new Dictionary<string, object>
                {
                    // 存入 code 和过期时间
                    {"phone",dto.phone},
                    {"time" , DateTime.Now.AddSeconds(60*60*12) }
                };

                //使用jwt token加密 获取token
                var token = Common.EncryptUtil.JWT(payload);

                result.data = new
                {
                    token
                };
            }

            return Ok(result);

        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult WxZhuCe(WxUserRequestDto dto)
        {

            var result = _loginService.WxZhuCe(dto);

            return Ok(result);

        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [Attributes.Auth]
        [HttpGet]
        public IHttpActionResult Getinfo()
        {
            var user = this.GetWxUser();
            if (user != null)
            {
                return Ok(new
                {
                    code = 0,
                    User = new
                    {
                        name = user.UName,
                        sex = user.USex,
                        id = user.Uid,
                        money = user.Umoney,
                        vip = user.Uvip
                    }
                });
            }
            else
            {
                return Ok(new
                {
                    code = -1,
                    message = "用户未登录"
                });
            }
        }
        /// <summary>
        /// 获取前台用户的信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetWxUserInfo(string id)
        {
            var result = _loginService.GetWxUserInfo(id);

            return Ok(result);
        }
    }




}
